package com.ruoyi.service.impl;

import com.ruoyi.domain.DailyUserStatistics;
import com.ruoyi.domain.MonthUserStatisticsDTO;
import com.ruoyi.domain.SaveRescue;
import com.ruoyi.domain.ServiceTypeCount;
import com.ruoyi.service.CarCorderService;
import com.ruoyi.mapper.CarCorderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;

/**
* @author LENOVO
* @description 针对表【car_corder(用户订单表)】的数据库操作Service实现
* @createDate 2025-08-07 14:46:18
*/
@Service
public class CarCorderServiceImpl implements CarCorderService{
    @Autowired
    private CarCorderMapper carCorderMapper;
    /**
     * 根据日期查询当天的订单总数
     */
    @Override
    public int countOrdersByDate(Date date) {
        return carCorderMapper.countOrdersByDate(date);
    }

    /**
     * 根据日期查询当天的订单总金额
     */
    @Override
    public BigDecimal sumOrderAmountByDate(Date date) {
        return carCorderMapper.sumOrderAmountByDate(date);
    }

    /**
     * 根据日期查询新增用户的数量
     */
    @Override
    public int userAddCount(Date date) {
        return carCorderMapper.userAddCount(date);
    }
    /**
     * 根据日期查询新增商户的数量
     */
    @Override
    public int merchantAddCount(Date date) {
        return carCorderMapper.merchantAddCount(date);
    }

    /**
     * 查询商户总数
     */
    @Override
    public int merchantCount() {
        return carCorderMapper.merchantCount();
    }

    /**
     * 获取用户总数
     */
    @Override
    public int userCount() {
        return carCorderMapper.userCount();
    }

    @Override
    public ServiceTypeCount countOrderByServiceType(Date date) {
        // 调用Mapper层查询数据，可在此处添加业务逻辑（如数据校验、格式化等）
        return carCorderMapper.countOrderByServiceType(date);
    }

    @Override
    public ServiceTypeCount countAllOrderByServiceType() {
        // 调用Mapper层查询数据，可在此处添加业务逻辑（如数据校验、格式化等）
        return carCorderMapper.countAllOrderByServiceType();
    }

    @Override
    public SaveRescue countSaveRescue(Date date) {
        // 调用Mapper层查询数据，可在此处添加业务逻辑（如数据校验、格式化等）
        return carCorderMapper.countSaveRescue(date);
    }

    @Override
    public SaveRescue countAllSaveRescue() {
        // 调用Mapper层查询数据，可在此处添加业务逻辑（如数据校验、格式化等）
        return carCorderMapper.countAllSaveRescue();
    }

    @Override
    public MonthUserStatisticsDTO getMonthlyUserRegistrationStats(int year, int month) {
        // 获取该月份的总天数
        int daysInMonth = LocalDate.of(year, month, 1).lengthOfMonth();

        // 从数据库查询该月的实际数据
        List<DailyUserStatistics> dailyStats = carCorderMapper.selectDailyNewUsersByMonth(year, month);

        // 构建日期标签和用户数据列表
        List<String> dateLabels = new ArrayList<>();
        List<Integer> userCounts = new ArrayList<>();
        int totalUsers = 0;

        // 将数据库数据存入Map，方便查询
        Map<Integer, Integer> dayCountMap = new HashMap<>();
        for (DailyUserStatistics stat : dailyStats) {
            dayCountMap.put(stat.getDay(), stat.getUserCount());
            totalUsers += stat.getUserCount();
        }

        // 填充每一天的数据（确保所有日期都有记录，无数据则为0）
        for (int i = 1; i <= daysInMonth; i++) {
            dateLabels.add(i + "日");
            userCounts.add(dayCountMap.getOrDefault(i, 0));
        }

        // 构建返回结果
        MonthUserStatisticsDTO result = new MonthUserStatisticsDTO();
        result.setYear(year);
        result.setMonth(month);
        result.setDateLabels(dateLabels);
        result.setUserCounts(userCounts);
        result.setTotalUsers(totalUsers);

        return result;
    }
}




